<?php
/**
 * $Author: BEESSHOP $
 * ============================================================================
 * ============================================================================
 */
namespace app\controllers;

use app\models\AdminRecorder;
use yii\web\Controller;

class LoginController extends Controller{
    public $enableCsrfValidation = false;

    public function actionLogin(){
        $post = \Yii::$app->request->post('form');
        $admin_name=$post['admin_name'];
        $admin_password = $post['admin_password'];
        if(empty($admin_name)){
            $result = ['result'=>0,'data'=>'请填写登录名'];
            return json_encode($result);
        }
        if(empty($admin_password)){
            $result = ['result'=>0,'data'=>'请填写登录密码'];
            return json_encode($result);
        }
        $model = AdminRecorder::find()->where('admin_name=:name',[':name' => $post['admin_name']])->one();
        if(!$model){
            $result = ['result'=>0,'data'=>'登录失败，请重新登陆'];
            return json_encode($result);
        }
        if(!\Yii::$app->security->validatePassword($post['admin_password'],$model->admin_password)){
            $result = ['result'=>0,'data'=>'登录失败，请重新登陆'];
            return json_encode($result);
        }
        \Yii::$app->admin->login($model);
        $result = ['result'=>1,'data'=>"login"];
        return json_encode($result);
    }

    public function actionIslogin(){
        if(\Yii::$app->admin->isGuest){
            $result = ['result'=>0,'data'=>'请先登录'];
            return json_encode($result);
        }
        $result = ['result'=>1,'data'=>'login'];
        return json_encode($result);
    }

    public function actionLoginout(){
        \Yii::$app->admin->logout();
        $result = ['result'=>"out"];
        return json_encode($result);
    }

}